﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BusinessLogicLayer;
using DataTransferObject;
using DevComponents.DotNetBar.Controls;

namespace PKSoftManagement
{
    public partial class MDIForm : Form
    {
        LoaiTrieuChungBLL BLL = new LoaiTrieuChungBLL();
        List<Control> Llist;
        int idBN;
        public MDIForm(int id, string hoten)
        {
            InitializeComponent();
            dataGridViewX1.AutoGenerateColumns = false;
            ma.Text = id.ToString();
            idBN = id;
            ten.Text = hoten;
            Llist = new List<Control>();
            DoTrieuChung();
        }
        private void superTabControl1_SelectedTabChanging(object sender, SuperTabStripSelectedTabChangingEventArgs e)
        {
            if (e.NewValue == trieuChungTab)
            {
                DoTrieuChung();
            }
            else if (e.NewValue == benhAnTab)
            {
                DoBenhAn();
            }
            if (e.OldValue == trieuChungTab)
            {
                info.Controls.Clear();
            }
            if (superTabControl1.Tabs.Contains("chiTiet") && superTabControl1.SelectedTab != superTabControl1.Tabs["chiTiet"])
            {
                superTabControl1.Tabs.Remove("chiTiet");
            }
        }
        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewX1.CurrentCell != null)
                if (dataGridViewX1.CurrentCell.OwningColumn.Name == "ctColumn")
                {
                    if (!superTabControl1.Tabs.Contains("chiTiet"))
                    {
                        UserControl uc = new ChiTietBenhAn(idBN, (int)dataGridViewX1.Rows[e.RowIndex].Cells["ID"].Value) { Dock = DockStyle.Fill };
                        uc.Show();
                        SuperTabItem newtab = superTabControl1.CreateTab("Chi Tiết");
                        newtab.Name = "chiTiet";
                        newtab.AttachedControl.Controls.Add(uc);
                    }
                    superTabControl1.SelectedTab = (SuperTabItem)superTabControl1.Tabs[superTabControl1.Tabs.Count - 1];
                }
        }
        private void save_Click(object sender, EventArgs e)
        {
            var list = new TrieuChungBanDauBLL().Get(idBN).ToList();
            foreach (Control c in Llist)
            {
                if ((c is TextBox && c.Text != string.Empty) || (c is CheckBox && (c as CheckBox).Checked == true))
                {
                    var f = list.Find(p => p.IdTrieuChung == (int)c.Tag);
                    if (f == null)
                    {
                        (new TrieuChungBanDauBLL()).Insert(new TrieuChungBanDauDto()
                        {
                            IdBenhNhan = idBN,
                            IdTrieuChung = (int)c.Tag,
                            GiaTri = (c is TextBox ? (c.Text == string.Empty ? null : c.Text) : null)
                        });
                    }
                    else
                    {
                        (new TrieuChungBanDauBLL()).Update(new TrieuChungBanDauDto()
                        {
                            Id = f.Id,
                            IdBenhNhan = idBN,
                            IdTrieuChung = (int)c.Tag,
                            GiaTri = (c is TextBox ? (c.Text == string.Empty ? null : c.Text) : null)
                        }, f.Id);
                    }
                }
                else
                    try
                    {
                        var find = list.Find(p => p.IdTrieuChung == (int)c.Tag);
                        if (find != null)
                        {
                            (new TrieuChungBanDauBLL()).Delete(find.Id, find.Id);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Lưu không thành công: " + ex.Message);
                        //save info error to log file or database
                        return;
                    }
            }
            MessageBox.Show("Lưu thành công");
        }
        private void addBenhAn_Click(object sender, EventArgs e)
        {
            if (!superTabControl1.Tabs.Contains("chiTiet"))
            {
                UserControl uc = new ChiTietBenhAn(idBN) { Dock = DockStyle.Fill };
                uc.Show();
                SuperTabItem newtab = superTabControl1.CreateTab("Chi Tiết");
                newtab.Name = "chiTiet";
                newtab.AttachedControl.Controls.Add(uc);
            }
            superTabControl1.SelectedTab = (SuperTabItem)superTabControl1.Tabs[superTabControl1.Tabs.Count - 1];
        }
        #region
        private void DoTrieuChung()
        {
            info.Controls.Clear();
            Llist.Clear();
            var list = BLL.GetAll().ToList();
            foreach (var item in list)
            {
                GroupBox temp = new GroupBox() { Text = (item as DmLoaiTrieuChungDto).Ten, AutoSize = true, Width = 250 };
                FlowLayoutPanel t = new FlowLayoutPanel() { FlowDirection = FlowDirection.TopDown, WrapContents = false, AutoScroll = true, AutoSize = true, Width = temp.Width - 10, Height = temp.Height - 20, Top = 15, Left = 5 };
                temp.Controls.Add(t);
                info.Controls.Add(temp);
                var listChild = (new DmTrieuChungBLL()).Get(item.Id).ToList();
                foreach (var itemChild in listChild)
                {
                    dynamic cb;
                    Panel fl = new Panel() { AutoSize = true };
                    Label lb = new Label() { Text = itemChild.Ten };
                    fl.Controls.Add(lb);
                    if (itemChild.IdType == 2)
                    {
                        cb = new CheckBox() { Left = 200, Tag = itemChild.Id, AutoSize = true };
                        if (new TrieuChungBanDauBLL().Get(idBN).ToList().Find(p => p.IdTrieuChung == itemChild.Id) != null)
                            cb.Checked = true;
                    }
                    else
                    {
                        cb = new TextBox() { Width = 30, Left = 185, Tag = itemChild.Id };
                        TrieuChungBanDauDto tf = new TrieuChungBanDauBLL().Get(idBN).ToList().Find(p => p.IdTrieuChung == itemChild.Id);
                        if (tf != null)
                            cb.Text = tf.GiaTri;
                    }
                    fl.Controls.Add(cb);
                    t.Controls.Add(fl);
                    Llist.Add(cb);
                }
            }
        }
        private void DoBenhAn()
        {
            try
            {
                var list = new BenhBanDau_NoiDungDieuTriBLL().GetWhereBenhNhan(idBN);
                dataGridViewX1.DataSource = list.ToList();
                SetHeaderColumn();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void SetHeaderColumn()
        {
            if (!dataGridViewX1.Columns.Contains("ctColumn"))
            {
                Image img = PKSoftManagement.Properties.Resources.edit;
                DataGridViewColumn column = new DataGridViewImageColumn() { HeaderText = "Chi Tiết", Name = "ctColumn", Image = img, ImageLayout = DataGridViewImageCellLayout.Zoom };
                dataGridViewX1.Columns.Add(column);
            }
        }
        protected override void WndProc(ref Message message)
        {
            const int WM_SYSCOMMAND = 0x0112;
            const int SC_MOVE = 0xF010;

            if (message.Msg == WM_SYSCOMMAND)
            {
                int command = message.WParam.ToInt32() & 0xFFF0;
                if (command == SC_MOVE && WindowState == FormWindowState.Minimized)
                    return;
            }
            base.WndProc(ref message);
        }
        #endregion

    }
}
